package com.scene.q1_Basic;

// 父类：基础商品
class Product {
    private String id;
    private String name;
    private double price;

    public Product(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // 计算折扣价（默认无折扣）
    public double getDiscountedPrice() {
        return price;
    }

    public void displayInfo() {
        System.out.printf("ID: %s | 商品名: %s | 价格: ¥%.2f\n",
                id, name, price);
    }
}

// 子类1：电子产品（新增保修期属性）
class Electronics extends Product {
    private int warrantyMonths; // 保修期

    public Electronics(String id, String name, double price, int warrantyMonths) {
        super(id, name, price);
        this.warrantyMonths = warrantyMonths;
    }

    // 电子产品有9折优惠
    @Override
    public double getDiscountedPrice() {
        return super.getDiscountedPrice() * 0.9;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.printf("保修期: %d个月 | 折后价: ¥%.2f\n",
                warrantyMonths, getDiscountedPrice());
    }
}

// 子类2：生鲜食品（新增保质期属性）
class FreshFood extends Product {
    private String expiryDate; // 保质期

    public FreshFood(String id, String name, double price, String expiryDate) {
        super(id, name, price);  // 必须调用父类构造器
        this.expiryDate = expiryDate; // 然后初始化子类特有属性
    }

    // 生鲜当天5折优惠
    @Override
    public double getDiscountedPrice() {
        return super.getDiscountedPrice() * 0.5;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.printf("保质期至: %s | 今日特价: ¥%.2f\n",
                expiryDate, getDiscountedPrice());
    }
}

// 测试类
class ECommerceDemo {
    public static void main(String[] args) {
        Product phone = new Electronics("P1001", "智能手机", 5999, 24);
        Product fish = new FreshFood("F2002", "三文鱼", 198, "2023-12-31");

        phone.displayInfo();
        System.out.println("-----");
        fish.displayInfo();

        // 多态应用示例
        Product[] inventory = {
                new Electronics("P1002", "蓝牙耳机", 299, 12),
                new FreshFood("F2003", "草莓", 25.8, "2023-12-25")
        };

        System.out.println("\n=== 库存商品 ===");
        for (Product p : inventory) {
            p.displayInfo();
        }
    }
}
